import 'nprogress/nprogress.css'
import { start, done, configure } from 'nprogress'
// 404页面
import NotFound from '@/views/NotFound.vue'

configure({ trickleRate: 0.1, trickleSpeed: 800 })

function delay(duration) {
	return new Promise((resolve) => {
		setTimeout(() => {
			resolve()
		}, duration)
	})
}

function getPageComponent(pageCompResolver) {
	return async () => {
		start()
		if (process.env.NODE_ENV === 'development') {
			await delay(50)
		}
		const comp = await pageCompResolver()
		done()
		return comp
	}
}

// 一个对象就是一个【路径--组件】的匹配信息
const routes = [
	{
		name: 'home',
		path: '/',
		component: getPageComponent(() => import('@/views/Home')),
		meta: {
			title: '首页',
		},
	},
	{
		name: 'blog',
		path: '/blog',
		component: getPageComponent(() => import(/* webpackChunkName:"blog" */ '@/views/Blog')),
		meta: {
			title: '文章',
		},
	},
	{
		name: 'categoryBlog',
		path: '/blog/cate/:categoryId',
		component: getPageComponent(() => import(/* webpackChunkName:"blog" */ '@/views/Blog')),
		meta: {
			title: '文章',
		},
	},
	{
		name: 'detail',
		path: '/blog/:id',
		component: getPageComponent(() => import(/* webpackChunkName:"detail" */ '@/views/Blog/components/Detail')),
		meta: {
			title: '文章详情',
		},
	},
	{
		name: 'about',
		path: '/about',
		component: getPageComponent(() => import('@/views/About')),
		meta: {
			title: '关于我',
		},
	},
	{
		name: 'message',
		path: '/message',
		component: getPageComponent(() => import('@/views/Message')),
		meta: {
			title: '留言板',
		},
	},
	{
		name: 'project',
		path: '/project',
		component: getPageComponent(() => import(/* webpackChunkName:"Project" */ '@/views/Project')),
		meta: {
			title: '项目',
		},
	},
	// 404页面
	{
		name: 'notFound',
		path: '/:pathMatch(.*)',
		component: NotFound,
	},
]

export default routes
